package com.lyw.login

import androidx.activity.viewModels
import androidx.core.widget.addTextChangedListener
import androidx.lifecycle.lifecycleScope
import com.alibaba.android.arouter.facade.annotation.Route
import com.blankj.utilcode.util.LogUtils
import com.blankj.utilcode.util.ToastUtils
import com.lu.library.base.BaseActivity
import com.lu.library.utils.EventBusUtils
import com.lyw.container.ContainerActivity
import com.lyw.login.bean.LoginEvent
import com.lyw.login.databinding.ActivityLoginBinding
import com.lyw.login.vm.LoginIntent
import com.lyw.login.vm.LoginUiState
import com.lyw.login.vm.LoginVm
import com.lyw.login_api.LoginRoutePaths
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.launch
import me.shouheng.utils.ktx.onDebouncedClick
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode

@Route(path = LoginRoutePaths.PATH_LOGIN)
@AndroidEntryPoint
class LoginActivity : BaseActivity<ActivityLoginBinding>() {

    private val loginVm by viewModels<LoginVm>()
    override fun initViews() {
        super.initViews()

        binding.tvLogin.setOnClickListener {
            loginVm.sendUiIntent(LoginIntent.LoginIn(binding.etUserName.text.toString(),binding.etPwd.text.toString()))
        }

//        ARouter.getInstance().addRouteGroup {
//            it[LoginRoutePaths.PATH_LOGIN] = RouteMeta.build(RouteType.ACTIVITY,
//                LoginActivity::class.java,LoginRoutePaths.PATH_LOGIN,"login",0,0)
//        }
        binding.tvRegister.setOnClickListener {
            startActivity(RegisterActivity::class.java)
        }
        binding.tvForgetPwd.setOnClickListener {
//            ToastUtils.showShort("功能开发中")
            startActivity(ForgetPwdActivity::class.java)
        }
        binding.tvLoginWithCode.onDebouncedClick {
            startActivity(LoginWithPhoneActivity::class.java)
        }
    }

    override fun initListener() {
        super.initListener()
        binding.etUserName.addTextChangedListener {
            loginVm.sendUiIntent(LoginIntent.UserNameChange(it.toString()))
        }
        binding.etPwd.addTextChangedListener {
            loginVm.sendUiIntent(LoginIntent.PwdChange(it.toString()))
        }
        EventBusUtils.register(this)
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    fun onLoginEvent(event: LoginEvent){
        finish()
    }
    override fun initData() {
        lifecycleScope.launch {

            LogUtils.d("collect start")
            loginVm.uiStateFlow.collect{

                LogUtils.d(Thread.currentThread())
                when(it){
                    is LoginUiState.INIT ->{
                        binding.etUserName.setText(it.userName)
                        binding.etPwd.setText(it.userName)
                    }

                    is LoginUiState.LoginSuccess -> {
                        ToastUtils.showShort("登录成功")
                        LogUtils.d(it.i);
                        startActivity(ContainerActivity::class.java)
                    }

                    is LoginUiState.InfoChange -> {
                        binding.tvLogin.isEnabled = it.loginEnable
                    }
                }
            }

            LogUtils.d("collect done")
        }

    }

    override fun layoutResId(): Int = R.layout.activity_login
}